<template>
  <div class="app-container bg-gray">
    <el-card class="box-1">
      <div slot="header">
        <span>已考统计</span>
        <el-button v-if="total>0" :loading="downloadLoading" type="success" class="bg-green" size="small" style="float: right;" @click="toExport">导出数据</el-button>
      </div>
      <el-row>
        <el-col :xs="12" :sm="12" :md="6" :lg="3" :xl="3">
          <el-card class="box-1 text-center" shadow="false">
            <div slot="header">
              <span>试卷总分</span>
            </div>
            <div>{{ analyseData['total_score'] || '--' }}</div>
          </el-card>
        </el-col>
        <el-col :xs="12" :sm="12" :md="6" :lg="3" :xl="3">
          <el-card class="box-1 text-center" shadow="false">
            <div slot="header">
              <span>考试次数</span>
            </div>
            <div>{{ analyseData['frequency'] || '--' }}</div>
          </el-card>
        </el-col>
        <el-col :xs="12" :sm="12" :md="6" :lg="3" :xl="3">
          <el-card class="box-1 text-center" shadow="false">
            <div slot="header">
              <span>平均分</span>
            </div>
            <div>{{ analyseData['avg'] || '--' }}</div>
          </el-card>
        </el-col>
        <el-col :xs="12" :sm="12" :md="6" :lg="3" :xl="3">
          <el-card class="box-1 text-center" shadow="false">
            <div slot="header">
              <span>最高分</span>
            </div>
            <div>{{ analyseData['max'] || '--' }}</div>
          </el-card>
        </el-col>
        <el-col :xs="12" :sm="12" :md="6" :lg="3" :xl="3">
          <el-card class="box-1 text-center" shadow="false">
            <div slot="header">
              <span>最低分</span>
            </div>
            <div>{{ analyseData['min'] }}</div>
          </el-card>
        </el-col>
        <el-col :xs="12" :sm="12" :md="6" :lg="3" :xl="3">
          <el-card class="box-1 text-center" shadow="false">
            <div slot="header">
              <span>及格分</span>
            </div>
            <div>{{ analyseData['qualified_score'] || '--' }}</div>
          </el-card>
        </el-col>
        <el-col :xs="12" :sm="12" :md="6" :lg="3" :xl="3">
          <el-card class="box-1 text-center" shadow="false">
            <div slot="header">
              <span>及格率</span>
            </div>
            <div>{{ analyseData['pass_rate'] || '--' }}</div>
          </el-card>
        </el-col>
        <el-col :xs="12" :sm="12" :md="6" :lg="3" :xl="3">
          <el-card class="box-1 text-center" shadow="false">
            <div slot="header">
              <span>及格次数</span>
            </div>
            <div>{{ analyseData['pass'] || '--' }}</div>
          </el-card>
        </el-col>
      </el-row>
    </el-card>
    <el-card class="box-1 margin-top-20">
      <div class="header">
        <div>
          <el-select v-model="searchTerms.status" placeholder="成绩状态" class="select" clearable @change="getData">
            <el-option :value="1" label="及格"/>
            <el-option :value="2" label="不及格"/>
          </el-select>
          <el-input v-model="searchTerms.searchValue" placeholder="请输入考生名称" class="search-input" @change="getData" />
          <el-button type="success" class="do-btn" icon="el-icon-search" @click="getData">搜索</el-button>
        </div>
      </div>
      <el-table v-loading="tableLoading" :data="tableData" :max-height="tableMaxHeight" border highlight-current-row @sort-change="handleSortChange">
        <el-table-column prop="id" label="ID" width="100" header-align="center" align="center" sortable />
        <el-table-column prop="name" label="姓名" min-width="100" header-align="center" align="center" />
        <!--<el-table-column prop="department" label="部门" min-width="100" header-align="center" align="center" />-->
        <el-table-column prop="start_time" label="开始时间" min-width="200" header-align="center" align="center"/>
        <el-table-column prop="end_time" label="交卷时间" min-width="200" header-align="center" align="center"/>
        <el-table-column prop="branch" label="最高成绩" min-width="100" header-align="center" align="center" />
        <el-table-column prop="number" label="考试次数" min-width="100" header-align="center" align="center" />
        <el-table-column label="状态" min-width="140" header-align="center" align="center">
          <template slot-scope="scope">
            <el-tag v-if="scope.row['status']===1" type="success">及格</el-tag>
            <el-tag v-if="scope.row['status']===0" type="danger">不及格</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" min-width="200" header-align="center" align="center">
          <template slot-scope="scope">
            <el-button type="text" size="small" icon="el-icon-search" @click="toAnswerSheetDetails(scope.row)">查看</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        :current-page.sync="currentPage"
        :page-size="pageSize"
        :page-sizes="pageSizes"
        :total="total"
        class="pagination"
        layout="total, sizes, prev, pager, next, jumper"
        @current-change="handleCurrentChange"
        @size-change="handleSizeChange"/>
    </el-card>
  </div>
</template>

<script>
import { handleSortChange, handleSizeChange, handleCurrentChange, parseTime } from '@/utils/table'

export default {
  data() {
    return {
      downloadLoading: false,
      searchTerms: {
        state: '',
        searchValue: ''
      },

      // 统计数据
      analyseData: {

      },

      // 考试id
      id: null,

      // 表格相关数据
      tableMaxHeight: document.documentElement.clientHeight - 380,
      tableLoading: false,
      multipleSelection: [],
      currentPage: 1,
      pageSize: this.$store.getters.userInfo.pageSize,
      pageSizes: [10, 20, 50, 100, 1000],
      total: 0,
      tableData: []
    }
  },
  created() {
    this.id = this.$route.query.id;
    this.getAnalyseData();
    this.getData();
  },
  methods: {
    handleCurrentChange, handleSizeChange, handleSortChange, parseTime,
    // 获取统计数据
    getAnalyseData(){
      const url = 'exam/examResultAnalyse/' + this.id;
      this.$store.dispatch('GetConnect', { url }).then(res => {
        this.analyseData = res.data
      }).catch(e => {
        this.$message.error(e.msg + ',请刷新或联系管理员')
      })
    },
    // 获取考生列表
    getData(order) {
      this.tableLoading = true
      order || (order = {})
      const url = 'exam/examResultList'
      const data = {
        id: this.id,
        pageNo: this.currentPage,
        pageSize: this.pageSize,
        search: this.searchTerms.searchValue,
        status: this.searchTerms.status,
        type: this.searchTerms.type
      }
      this.searchTerms.date && this.searchTerms.date.length !== 0 && (data.start_time = this.searchTerms.date[0] + ' 00:00:00', data.end_time = this.searchTerms.date[1] + ' 23:59:59')
      order.sortName && (data.sortName = order.sortName)
      order.sortOrder && (data.sortOrder = order.sortOrder)
      this.$store.dispatch('GetConnect', { url, data }).then(res => {
        this.tableData = res.data.rows
        this.total = res.data.total
        this.tableLoading = false
      }).catch(e => {
        this.tableLoading = false
        this.$message.error(e.msg + ',请刷新或联系管理员')
      })
    },

    toAnswerSheetDetails(row) {
      this.$router.push({
        path: '/exam/answerSheetDetails',
        query: {
          id: row.id,
          uid: row.uid,
          type: row.type
        }
      })
    },

    toExport() {
      this.downloadLoading = true;
      window.open(`${this.$store.getters.serverUrl}/admin/exam/userExamToExcel?token=${this.$store.getters.token}&id=${this.id}&mallId=${this.$store.getters.SaaSId}`)
      this.downloadLoading = false;
    }
  }
}
</script>

<style lang="scss" type="text/scss" scoped>
@import '../../../../styles/index.scss';
.box-1 {
  .model {
    .el-card {
      color: #fff;
      background-color: $panGreen;
      span {
        font-size: 14px;
      }
      div {
        font-size: 24px;
        margin-top: 10px;
      }
    }
  }
  .header {
    @include flex-row(center, space-between);
    flex-wrap: wrap;
    padding-bottom: 15px;
    div {
      @include flex-row(center, flex-start);
      flex-wrap: wrap;
      .select {
        padding-right: 15px;
        width: 150px;
      }
      .picker {
        margin-right: 15px;
        width: 300px;
      }
      .do-btn {
        @extend .bg-green;
        margin-left: 6px;
      }
      .search-input {
        width: 180px;
      }
    }
  }
}
</style>

